﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyBanHang.DAL
{
    public class KhachHang_DAL
    {
        dbConnection conn;
        public KhachHang_DAL()
        {
            conn = new dbConnection();
        }
        public DataTable Select(string key)
        {
            string query = "select * from KHACHHANG where TenKH like '%'+@ten+'%'";
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter("@ten", key);
            return conn.execSelectQuery(query, p);
        }
        public bool insert(string tenkh, string dc, string email,string sodt)
        {
            string query = "insert into KHACHHANG values(@tenkh,@dc,@email,@sodt)";
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@tenkh", tenkh);
            p[1] = new SqlParameter("@dc", dc);
            p[2] = new SqlParameter("@email",email );
            p[3] = new SqlParameter("@sodt", sodt);            
            return conn.execInsertQuery(query, p);
        }
        public bool update(int makh, string tenkh, string dc, string email, string sodt)
        {
            string query = "update KHACHHANG set TenKH=@tenkh,DiaChi=@dc,Email=@email,SoDT=@sodt where MaKH=@makh";
            SqlParameter[] p = new SqlParameter[5];
            p[0] = new SqlParameter("@tenkh", tenkh);
            p[1] = new SqlParameter("@dc",dc);
            p[2] = new SqlParameter("@email",email);
            p[3] = new SqlParameter("@sodt", sodt);            
            p[4] = new SqlParameter("@makh", makh);
            return conn.execUpdateQuery(query, p);
        }
        public bool delete(int makh)
        {
            string query = "delete from KHACHHANG where MaKH =@makh";
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter("@makh", makh);
            return conn.execDeleteQuery(query, p);
        }
        public DataTable laykhachhang()
        {

            return conn.execSelectProcedureQueryNonParameter("sp_laykhachhang");
        }
        public DataTable timkiemkh(string tenkh)
        {
            SqlParameter[]p=new SqlParameter[1];
            p[0]=new SqlParameter("@tenkh",tenkh);
            return conn.execSelectProcedureQuery("sp_timkiemkh",p );
        }
        public bool themkh(string tenkh, string diachi, string email, string sodt)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@tenkh", tenkh);
            p[1] = new SqlParameter("@diachi", diachi);
            p[2] = new SqlParameter("@email", email);
            p[3] = new SqlParameter("@sodt", sodt);
            return conn.execInsertProcedureQuery("sp_themkh", p);
        }
        public DataTable timmakh(string makh)
        {
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter("@makh", makh);
            return conn.execSelectProcedureQuery("sp_timmakh", p);
        }
        public bool suakhachhang(int makh, string tenkh, string diachi, string email, string sodt)
        {
            SqlParameter[] p = new SqlParameter[5];
            p[0]=new SqlParameter("@makh",makh);
            p[1] = new SqlParameter("@tenkh", tenkh);
            p[2] = new SqlParameter("@diachi", diachi);
            p[3] = new SqlParameter("@email", email);
            p[4] = new SqlParameter("@sodt", sodt);
            return conn.execUpdateProcedureQuery("sp_suakhachhang", p);
        }
        public bool xoakhachhang(int makh)
        {
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter("@makh", makh);
            return conn.execDeleteProcedureQuery("sp_xoakhachhang", p);
        }
    }
}
